﻿/********************************************************************
	日期:2022/03/27
	名称:GameRoot
	邮箱:yunnibbd@163.com
	作者:yunni
	描述:游戏启动入口
*********************************************************************/

using PEProtocol;
using UnityEngine;

public class GameRoot : MonoBehaviour{
    public LoadingWnd loadingWnd;
    public DynamicWnd dynamicWnd;

    public static GameRoot Instance;

    // Use this for initialization
    void Start(){
        Instance = this;
        DontDestroyOnLoad(this);
        PECommon.Log("Game start...");
        ClearUIRoot();
        Init();
    }

    private void ClearUIRoot(){
        Transform canvvas = transform.Find("Canvas");
        for (int i = 0; i < canvvas.childCount; ++i){
            canvvas.GetChild(i).gameObject.SetActive(false);
        }
    }

    private void Init(){
        //服务模块初始化
        NetSvc netSvc = GetComponent<NetSvc>();
        netSvc.InitSvc();
        ResSvc resSvc = GetComponent<ResSvc>();
        resSvc.InitRes();
        AudioSvc audio = GetComponent<AudioSvc>();
        audio.InitSvc();
        TimerSvc timer = GetComponent<TimerSvc>();
        timer.InitSvc();

        //业务模块初始化
        LoginSys loginSys = GetComponent<LoginSys>();
        loginSys.InitSys();
        MainCitySys mainCitySys = GetComponent<MainCitySys>();
        mainCitySys.InitSys();
        FuBenSys fuBenSys = GetComponent<FuBenSys>();
        fuBenSys.InitSys();
        BattleSys battleSys = GetComponent<BattleSys>();
        battleSys.InitSys();

        dynamicWnd.SetWndState();
        
        //进入登陆场景并加载对应ui
        loginSys.EnterLogin();
    }

    public static void AddTips(string tips){
        Instance.dynamicWnd.AddTips(tips);
    }

    private PlayerData playerData = null;

    public PlayerData PlayerData{
        get{ return playerData; }
    }

    public void SetPlayerData(ResLogin data){
        playerData = data.playerData;
    }

    public void SetPlayerName(string name){
        playerData.name = name;
    }

    public void SetPlayerDataByGuide(ResGuide data){
        PlayerData.coin = data.coin;
        PlayerData.level = data.lv;
        PlayerData.exp = data.exp;
        PlayerData.guideid = data.guideid;
    }

    public void UpdatePlayerDataByStrong(ResStrong data){
        PlayerData.coin = data.coin;
        PlayerData.crystal = data.crystal;
        PlayerData.hp = data.hp;
        PlayerData.ad = data.ad;
        PlayerData.ap = data.ap;
        PlayerData.addef = data.addef;
        PlayerData.apdef = data.apdef;
        PlayerData.strongArr = data.strongArr;
    }
    
    public void UpdatePlayerDataByBuy(ResBuy data){
        PlayerData.coin = data.coin;
        PlayerData.diamond = data.dimond;
        PlayerData.power = data.power;
    }

    public void SetPlayerDataByPower(PshPower data){
        playerData.power = data.power;
    }
    
    public void SetPlayerDataByTask(ResTakeTaskReward data){
        PlayerData.coin = data.coin;
        PlayerData.level = data.lv;
        PlayerData.exp = data.exp;
        PlayerData.taskArr = data.taskArr;
    }

    public void SetPlayerDataByTaskPsh(PshTaskPrg data){
        PlayerData.taskArr = data.taskArr;
    }
    
    public void SetPlayerDataByFBStart(ResBFFight data){
        PlayerData.power = data.power;
    } 
}
